﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinLogicUniversity
{
    public partial class frmPurchaseOrder : Form
    {
        string sID;
        string userID;
        public frmPurchaseOrder()
        {
            InitializeComponent();
        }

        private void frmPurchaseOrder_Load(object sender, EventArgs e)
        {
            cboSupplier.DisplayMember = "SName";
            cboSupplier.ValueMember = "SID";
            cboSupplier.DataSource =PurchaseManager.getAllSupplier(); 
        }

        private void cboSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            sID = cboSupplier.SelectedValue.ToString();
            DBBroker.PurchaseOrderSend form = new DBBroker.PurchaseOrderSend(sID);
            //PurchaseManager form = new  PurchaseManager(sID);
            dgpurchaseorder.DataSource =PurchaseManager.getPurchaseOrderList();
            lblTotalAmount.Text =PurchaseManager.getTotal().ToString();
        }
        public List<Object[]> getGridData()
        {
            String itemNo = "";
            String des = "";
            String qty = "";
            String price = "";
            String amount = "";
            List<Object[]> data = new List<Object[]>();
            foreach (DataGridViewRow row in dgpurchaseorder.Rows)
            {
                if (row != null)
                {
                    itemNo = row.Cells[0].Value.ToString();
                    des = row.Cells[1].Value.ToString();
                    qty = row.Cells[2].Value.ToString(); ;
                    price = row.Cells[3].Value.ToString();
                    amount = row.Cells[4].Value.ToString();
                    data.Add(new object[] { itemNo, des, qty, price, amount });
                    return data;
                }
                else
                {
                    MessageBox.Show("No data in gridview", "DataError");
                    return null;
                }
            }
            return data;
        }

        private void btnSendEmail_Click(object sender, EventArgs e)
        {
            userID = "E0007";
            PurchaseManager manager = new PurchaseManager(sID, userID);
            List<Object[]> list = getGridData();
            PurchaseManager ma = new PurchaseManager(list);
            PurchaseManager.sendEmail(sID, userID);
        }
    }
}
